/*
 * @(#)TowerController.java    Aug 20, 2010
 *
 * Copyright 2009 Nyber(Shanghai Itd. All rights reserved.
 */

package com.lab.puzzle;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.lang.StringUtils;

/**
 * 假定有一座观光塔, 只有一部电梯, 只能容纳一个游客上观光塔或下观光塔. 观光塔观光平台只能最多容纳5个人.
 *
 * @author nickevin@gmail.com
 * @version Aug 20, 2010 5:37:34 PM
 */

public class Elevator {

    private final static Elevator instance = new Elevator();

    private final static int THRESHOLD = 5;
    private final static BlockingQueue<Visitor> tower = new LinkedBlockingQueue<Visitor>(
            THRESHOLD);
    ExecutorService service = Executors.newCachedThreadPool();

    private Elevator() {}

    public static Elevator getInstance() {
        return instance;
    }

    public void rise(Visitor visitor) {
        service.submit(new GetUpThread(tower, visitor));
    }

    public void down() {
        service.submit(new GetOffThread(tower));
    }
}

class GetUpThread implements Runnable {

    private final BlockingQueue<Visitor> tower;
    private Visitor visitor;

    GetUpThread(BlockingQueue<Visitor> tower, Visitor visitor) {
        this.tower = tower;
        this.visitor = visitor;
    }

    public void run() {
        try {
            while (true) {
                System.out.println(StringUtils.center(
                        "当前塔上人数: " + tower.size(), 80, "-"));
                tower.put(produce());
                System.out.println(visitor + " 正在上塔, 请注意安全.");

                Thread.sleep(1000);
            }
        } catch (InterruptedException ex) {
        }
    }

    private Visitor produce() {
        return visitor;
    }

}

class GetOffThread implements Runnable {

    private final BlockingQueue<Visitor> tower;

    GetOffThread(BlockingQueue<Visitor> tower) {
        this.tower = tower;
    }

    public void run() {
        try {
            while (true) {
                System.out.println(StringUtils.center(
                        "当前塔上人数: " + tower.size(), 80, "-"));
                consume(tower.take());

                Thread.sleep(1000);
            }
        } catch (InterruptedException ex) {
        }
    }

    private void consume(Visitor visitor) {
        System.out.println(visitor + " 正在下塔, 请注意安全.");
    }

}

class Visitor {

    private String cardId;
    private String name;

    public Visitor(String cardId, String name) {
        this.cardId = cardId;
        this.name = name;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (cardId == null ? 0 : cardId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Visitor other = (Visitor) obj;
        if (cardId == null) {
            if (other.cardId != null) {
                return false;
            }
        } else if (!cardId.equals(other.cardId)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return new StringBuilder("游客: " + name).toString();
    }

}
